package it.univaq.mancoosi.injectors.systemconfiguration.managers;

import it.univaq.mancoosi.injectors.systemconfiguration.managers.ubuntu.UbuntuEnvironmentManager;
import it.univaq.mancoosi.mancoosimm.File;
import it.univaq.mancoosi.mancoosimm.GConf;
import it.univaq.mancoosi.mancoosimm.MancoosiFactory;
import it.univaq.mancoosi.mancoosimm.MenuEntry;
import it.univaq.mancoosi.mancoosimm.MenuSection;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

public abstract class GConfManager {
	
	protected static GConfManager INSTANCE;
	protected MancoosiFactory factory; 
	//protected GConf gconf;
	protected HashMap<String, File> gconfMap; 
	protected HashMap<String, File> schemaMap;

	protected GConfManager(){
		factory = MancoosiFactory.eINSTANCE;
	//	gconf = factory.createGConf(); 
		this.gconfMap = new HashMap<String, File>();
		this.schemaMap = new HashMap<String, File>();
	}

	public static GConfManager getInstance() {
		return INSTANCE;
	}

	public abstract void createGConfFromSystem();
	
	public abstract Boolean existsGConf(String gconfName);
	
	public abstract Boolean existsSchema(String schemaName);

	public abstract void synchronize() throws IOException;
	
	public void syncGConfMap () {
		Iterator<File> iterator  = null;
		if(UbuntuEnvironmentManager.getInstance().getEnvironment().getGconf() != null){
			iterator = UbuntuEnvironmentManager.getInstance().getEnvironment().getGconf().getConfFiles().iterator();
		}
		if(iterator != null){
			while (iterator.hasNext()) {
				File curr = iterator.next();
				this.gconfMap.put(curr.getLocation(), curr);
			}
		}
	}
	
	public void syncSchemaMap () {
		Iterator<File> iterator  = null;
		if(UbuntuEnvironmentManager.getInstance().getEnvironment().getGconf() != null){
			iterator = UbuntuEnvironmentManager.getInstance().getEnvironment().getGconf().getSchemas().iterator();
		}
		if(iterator != null){
			while (iterator.hasNext()) {
				File curr = iterator.next();
				this.schemaMap.put(curr.getName(), curr);
			}
		}
	}
}
